<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/2/5
 * Time: 下午12:29
 */
$uniacid = $_W['uniacid'];
$op = empty($_GPC['op']) ? '' : $_GPC['op'];
if ($op == 'list') {
    $start_day = $_GPC['start_day'];
    $finish_day = $_GPC['finish_day'];
    $adult_number = $_GPC['adult_number'];
    $child_number = $_GPC['child_number'];
    if (empty($start_day)) message('请选择出发日期', $this->createMobileUrl('insurance'), 'error');
    if (empty($finish_day)) message('请选择返回日期', $this->createMobileUrl('insurance'), 'error');
    if ((empty($adult_number) || $adult_number == 0) && (empty($child_number) || $child_number == 0)) message('请选择被保人数', $this->createMobileUrl('insurance'), 'error');

    //计算出行天数
    $start_day = strtotime($start_day);
    $finish_day = strtotime($finish_day) + 86400 - 1;
    $day = ceil(($finish_day - $start_day) / 86400);
    $insurances = pdo_fetchall("SELECT i.*,ip.id as ip_id,ip.adult_price,ip.child_price FROM" . tablename('bontrip_insurance')
        . " AS i LEFT JOIN " . tablename('bontrip_insurance_price') . " AS ip ON ip.insurance_id = i.id"
        . " WHERE i.uniacid = '{$uniacid}' AND ip.start_days <= {$day} AND ip.end_days >= {$day} AND i.deleted_at is null");
    $title = "旅游保险";
    include $this->template('insurance_list');
} elseif ($op == 'detail') {
    $id = intval($_GPC['id']);
    $start_day = $_GPC['start_day'];
    $finish_day = $_GPC['finish_day'];
    $adult_number = intval($_GPC['adult_number']);
    $child_number = intval($_GPC['child_number']);
    $type = trim($_GPC['type']);
    $index = intval($_GPC['index']);
    $insured_id = intval($_GPC['insured_id']);
    $member = unserialize(base64_decode($_SESSION['vj_member']));
    if (empty($member)) {
        header("location: " . $this->createMobileUrl('register'));
        exit;
    }
    if (empty($start_day)) message('请选择出发日期', $this->createMobileUrl('insurance'), 'error');
    if (empty($finish_day)) message('请选择返回日期', $this->createMobileUrl('insurance'), 'error');
    if (empty($id)) message('参数错误', $this->createMobileUrl('insurance'), 'error');

    if (empty($type)) {
        $_SESSION['insured'] = null;
    } else {
        $insured_info = $_SESSION['insured'];
        //获取被保人信息
        $insured = pdo_fetch("SELECT * FROM " . tablename('bontrip_insurance_member')
            . " WHERE member_id = {$member['id']} AND uniacid = {$uniacid} AND id = {$insured_id} AND deleted_at is null");
        $insured_info[$type][$index]['id'] = $insured['id'];
        $insured_info[$type][$index]['real_name'] = $insured['real_name'];
        $_SESSION['insured'] = $insured_info;
    }
    $insurance = pdo_fetch("SELECT i.*,ip.id as ip_id,ip.adult_price,ip.child_price FROM " . tablename('bontrip_insurance')
        . " AS i LEFT JOIN " . tablename('bontrip_insurance_price') . " AS ip ON ip.insurance_id = i.id"
        . " WHERE i.uniacid = '{$uniacid}' AND ip.id = {$id} AND i.deleted_at is null");
    if (empty($insurance)) message('保险不存在', $this->createMobileUrl('insurance'), 'error');
    $security_desc = unserialize($insurance["security_desc"]);
    $security = array();
    //默认取头两条进行呈现
    if (!empty($security_desc)) {
        for ($i = 0; $i < 2; $i++) {
            if (!empty($security_desc['security'][$i])) {
                $security[$i] = $security_desc['security'][$i];
            }
        }
    }
    if ($adult_number != 0) {
        $adult = range(1, $adult_number);
    }
    if ($child_number != 0) {
        $child = range(1, $child_number);

    }

    $is_insured = ((count($insured_info['adult']) != $adult_number) || count($insured_info['child']) != $child_number) ? '0' : '1';
    $day = ceil(($finish_day - $start_day) / 86400);

    //计算金额
    $insurance['total_amount'] = number_format(($adult_number * $insurance['adult_price']) + ($child_number * $insurance['child_price']), 2);

    $member = unserialize(base64_decode($_SESSION['vj_member']));
    $title = "填写订单";
    include $this->template('insurance_detail');
} elseif ($op == 'product') {
    $member = unserialize(base64_decode($_SESSION['vj_member']));
    if (empty($member)) {
        header("location: " . $this->createMobileUrl('register'));
        exit;
    }
    $id = intval($_GPC['id']);
    $insurance = pdo_fetch("SELECT i.*,ip.id as ip_id,ip.adult_price,ip.child_price FROM " . tablename('bontrip_insurance')
        . " AS i LEFT JOIN " . tablename('bontrip_insurance_price') . " AS ip ON ip.insurance_id = i.id"
        . " WHERE i.uniacid = '{$uniacid}' AND ip.id = {$id} AND i.deleted_at is null");
    if (empty($insurance)) message('保险不存在', $this->createMobileUrl('insurance'), 'error');
    $insurance["security_desc"] = unserialize($insurance["security_desc"]);
    $insurance["notice_desc"] = unserialize($insurance["notice_desc"]);
    include $this->template('product_interpretation');
} elseif ($op == 'payment') {
    $id = $_GPC['id'];
    $member = unserialize(base64_decode($_SESSION['vj_member']));
    if (empty($member)) {
        header("location: " . $this->createMobileUrl('register'));
        exit;
    }
    if (empty($id)) message('参数错误', $this->createMobileUrl('me'), 'error');
    $insurance_order = pdo_fetch("SELECT * FROM "
        . tablename('bontrip_insurance_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
    if (empty($insurance_order)) message('订单不存在', url('entry', array('m' => 'vj_bontrip', 'do' => 'order', 'op' => 'list', 'type' => 'insurance'), true), 'error');
    //计算出行天数
    $day = ceil(($insurance_order['finish_day'] - $insurance_order['start_day']) / 86400);
    $security_desc = unserialize($insurance_order["security_desc"]);
    $security = array();
    //默认取头两条进行呈现
    if (!empty($security_desc)) {
        for ($i = 0; $i < 2; $i++) {
            if (!empty($security_desc['security'][$i])) {
                $security[$i] = $security_desc['security'][$i];
            }
        }
    }
    if ($insurance_order['adult_number'] > 0) {
        $insurance_order['adult_members'] = unserialize($insurance_order['adult_members']);
    }
    if ($insurance_order['child_number'] > 0) {
        $insurance_order['child_members'] = unserialize($insurance_order['child_members']);
    }
    include $this->template('insurance_payment');
} else {
    $title = "旅游保险";
    include $this->template('insurance');
}


